import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_demo/widget/buttons.dart';
import 'package:flutter_demo/widget/choose_file.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'other/application.dart';

class CompanyRenameScreen extends StatelessWidget {

  final GlobalKey _formKey = GlobalKey<FormState>();
  final TextEditingController _nameController = TextEditingController();

  @override
  Widget build(BuildContext context) {

    var et_account = Padding(
      padding: const EdgeInsets.fromLTRB(64, 32, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          autofocus: true,
          decoration: const InputDecoration(
            labelText: '统一社会信用代码',
            hintText: '您的社会信用代码',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '统一社会信用代码不能为空';
            } else if (value.trim().length > 18) {
              return '统一社会信用代码超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var et_name = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          controller: _nameController,
          autofocus: true,
          decoration: const InputDecoration(
            labelText: '单位名称',
            hintText: '您的单位名称',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '单位名称不能为空';
            } else if (value.trim().length > 10) {
              return '单位名称超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var et_client = Padding(
      padding: const EdgeInsets.fromLTRB(64, 0, 64, 32),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: TextFormField(
          autofocus: true,
          decoration: const InputDecoration(
            labelText: '单位委托人名称',
            hintText: '您的单位委托人名称',
            border: InputBorder.none,
          ),
          validator: (value) {
            if (value!.trim().isEmpty) {
              return '单位委托人名称不能为空';
            } else if (value.trim().length > 4) {
              return '单位委托人名称超出最大长度';
            } else {
              return null;
            }
          },
        ),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.black26, width: 1.0),
          borderRadius: BorderRadius.circular(5.0),
        ),
      ),
    );

    var chooseLicense = Container(
      alignment: Alignment.center,
      child: ChoosePictureView(),
    );

    var choosePicture = Container(
      alignment: Alignment.center,
      child: ChoosePictureView(),
    );

    return Scaffold(
      appBar: AppBar(
        title: const Text('修改公司名称'),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            et_account,
            et_name,
            et_client,
            Container(
              margin: EdgeInsets.fromLTRB(64, 0, 64, 32),
              child: Row(
                children: [
                  Expanded(child: chooseLicense),
                  Expanded(child: choosePicture),
                  Expanded(
                    child: BaseButton(
                      radius: 12,
                      width: 180,
                      onTap: () {
                        if ((_formKey.currentState as FormState).validate())
                          validatePassed(context);
                      },
                      child: Text(
                        '完成',
                        style: TextStyle(
                          fontSize: 16,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  void validatePassed(BuildContext context) async {

    companyInfoData['单位名称'] = _nameController.text;

    Timer(Duration(milliseconds: 500), (){
      Fluttertoast.showToast(msg: '提交成功');
      Navigator.pop(context);
    });
  }
}